package com.cooper.service.core;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.constants.DictConstants;
import com.cooper.entity.Dict;
import com.cooper.resource.IResourceManager;
import com.google.common.collect.Maps;

@Service
public class DictService {
	
	private final static String DICT_CODE = "dict_code";
	
	@Autowired
	private IResourceManager resourceManager;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Dict> getDictList(String dictCode){
		List ret = resourceManager.getResListByGroup(DictConstants.DICT_INFO, DICT_CODE, dictCode);
		return ret==null?new ArrayList<Dict>():ret;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Dict getDict(String dictCode,String dictName){
		List ret = resourceManager.getResListByGroup(DictConstants.DICT_INFO, DICT_CODE, dictCode);
		if(ret!=null&&!ret.isEmpty()){
			for(Dict dict:(List<Dict>)ret){
				if(dict.getDictName().equals(dictName)){
					return dict;
				}
			}
		}
		return null;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Dict> getDictList(String dictCode,List<String> dictNameList){
		List<Dict> finalRet = new ArrayList<Dict>();
		List ret = resourceManager.getResListByGroup(DictConstants.DICT_INFO, DICT_CODE, dictCode);
		if(ret!=null&&!ret.isEmpty()){
			for(Dict dict:(List<Dict>)ret){
				if(dictNameList.contains(dict.getDictName())){
					finalRet.add(dict);
				}
			}
		}
		return finalRet;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map<String,Object> getDictMap(String dictCode){
		Map<String, Object> retMap = Maps.newHashMap();
		List ret = resourceManager.getResListByGroup(DictConstants.DICT_INFO, DICT_CODE, dictCode);
		if(ret!=null&&!ret.isEmpty()){
			for(Dict dict:(List<Dict>)ret){
				retMap.put(dict.getDictName(), dict.getDictValue());
			}
		}
		return retMap;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map<String,Object> getDictMap(String dictCode,List<String> dictNameList){
		Map<String, Object> retMap = Maps.newHashMap();
		List ret = resourceManager.getResListByGroup(DictConstants.DICT_INFO, DICT_CODE, dictCode);
		if(ret!=null&&!ret.isEmpty()){
			for(Dict dict:(List<Dict>)ret){
				if(dictNameList.contains(dict.getDictName())){
					retMap.put(dict.getDictName(), dict.getDictValue());
				}
			}
		}
		return retMap;
	}
	
	public Map<Integer, String> makeIntOptionMap(String dictCode){
		Map<Integer, String> retMap = Maps.newLinkedHashMap();
		List<Dict> options = this.getDictList(dictCode);
		if(options==null||options.isEmpty()){
			return retMap;
		}
		for(Dict dict:options){
			retMap.put(Integer.parseInt(dict.getDictName()), dict.getDictValue());
		}
		return retMap;
	}
	
	public LinkedHashMap<String, String> makeOptionList(String dictCode, 
			String defaultOptionName, String defaultOptionValue){
		LinkedHashMap<String, String> retMap = Maps.newLinkedHashMap();
		if(null != defaultOptionName && null != defaultOptionValue){
			retMap.put(defaultOptionName, defaultOptionValue);
		}
		List<Dict> options  = this.getDictList(dictCode);
		if(options==null||options.isEmpty()){
			return retMap;
		}
		
		for(Dict dict:options){
			retMap.put(dict.getDictName(), dict.getDictValue());
		}
		return retMap;
	}
	
}
